﻿using System.Windows.Controls;
using CCF20Model;
using WiseLinkWPF.Windows.Controls.GridView;
using System.Collections;
using System.Windows;
using WiseLinkWPF.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Data;
using WiseLinkWPF.Windows.Data;
using System.ComponentModel;
using System;
namespace CCF20
{
    /// <summary>
    /// Interaction logic for CustomizeMaintenance.xaml
    /// </summary>
    public partial class CustomizeMaintenance : UserControl
    {
        CustomizeModel customizeModel;
        public static readonly DependencyProperty TableNameProperty = DependencyProperty.Register("TableName", typeof(string), typeof(CustomizeMaintenance), null);
        public string TableName
        {
            get
            {
                return (string)GetValue(TableNameProperty);
            }

            set
            {
                SetValue(TableNameProperty, value);
            }
        }
        public CustomizeMaintenance()
        {
            InitializeComponent();
            customizeModel = new CustomizeModel();
        }

        
        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ListHelp listHelp = new ListHelp();
            switch (TableName)
            {
                case "VehicleType":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetVehicleType();
                    this.radMaintenanceRecord.ShowInsertRow = false;
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForVehicleType());
                    break;
                
                case "MaintenanceType":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetMaintenanceType();
                    this.radMaintenanceRecord.ShowInsertRow = false;
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForMaintenanceType());
                    break;
                case "SMSTemplate":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetSMSTemplate();
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForSMSTemplate());
                    break;
                case "CustomizedTroubleCode":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetCustomizedTroubleCode();
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForCustomizedTroubleCode());
                    break;
                case "RemindList":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetRemindList();
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForRemindList());
                    break;
                case "SMSHistory":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetSMSHistory();
                    this.radMaintenanceRecord.ShowInsertRow = false;
                    AddColumnToGrid(this.radMaintenanceRecord, listHelp.GetColumnForSMSHistory());
                    break;
                    
                
            }
        }
        private void AddColumnToGrid(RadGridView grid, List<GridColumn> columnList)
        {
            foreach (GridColumn column in columnList)
            {
                GridViewDataColumn newcolumn = new GridViewDataColumn();
                newcolumn.DataMemberBinding = new Binding(column.Binding);
                newcolumn.Header = column.Header;
                newcolumn.UniqueName = column.Header;
                newcolumn.TextTrimming = TextTrimming.CharacterEllipsis;
                newcolumn.TextWrapping = TextWrapping.Wrap;
                newcolumn.MaxWidth = 600;
                newcolumn.IsReadOnly = column.IsReadOnly;
                grid.Columns.Add(newcolumn);
            }
        }
        private void radMaintenanceRecord_RowEditEnded(object sender, WiseLinkWPF.Windows.Controls.GridViewRowEditEndedEventArgs e)
        {
            if (e.EditAction == GridViewEditAction.Commit)
            {
                customizeModel.Update();
                //SortDescriptor descriptor = new SortDescriptor();
                //descriptor.Member = this.TableName+"ID";
                //descriptor.SortDirection = ListSortDirection.Descending;
                //this.radMaintenanceRecord.SortDescriptors.Clear();
                //this.radMaintenanceRecord.SortDescriptors.Add(descriptor);
            }else if (e.EditOperationType == GridViewEditOperationType.Insert)
            {
                ((IList)this.radMaintenanceRecord.ItemsSource).Add(e.EditedItem);
                
            }else if (e.EditOperationType == GridViewEditOperationType.Edit)
            {
                customizeModel.Update();
            }
            
        }
        private void RadContextMenu_ItemClick(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
            RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
            GridViewRow row = menu.GetClickedElement<GridViewRow>();

            //radMaintenanceRecord.SelectedItem = row;
            if (clickedItem != null && row != null)
            {
                string header = Convert.ToString(clickedItem.Header);

                if (header == "删除")
                {

                    ((IList)this.radMaintenanceRecord.ItemsSource).Remove(row.Item);
                    this.radMaintenanceRecord.Rebind();

                }
                else if (header == "复制")
                {

                    switch (TableName)
                    {
                        case "MaintenanceType":
                            MaintenanceType mt = row.Item as MaintenanceType;
                            MaintenanceType newMT = new MaintenanceType();
                            newMT.Name = mt.Name;
                            newMT.ServiceMileage = mt.ServiceMileage;
                            newMT.ServicePeriod = mt.ServicePeriod;
                            newMT.VehicleTypeID = mt.VehicleTypeID;
                            newMT.Priority = mt.Priority == null ? 2: (short?)(mt.Priority+1);
                            newMT.TyID = mt.TyID;
                            newMT.AnchiID = mt.AnchiID;
                            ((IList)this.radMaintenanceRecord.ItemsSource).Add(newMT);
                            customizeModel.Update();
                            break;
                        default:
                            return;
                    }


                    this.radDataPagerMaintenanceRecord.MoveToLastPage();
                    this.radMaintenanceRecord.Items.MoveCurrentToLast();

                }
            }
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            ListHelp listHelp = new ListHelp();
            CCFZIC ccfZIC;
            switch (TableName)
            {
                case "VehicleType":
                    ccfZIC = new CCFZIC();
                    ccfZIC.GetVehicleType();
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetVehicleType();
                    break;
                
                case "MaintenanceType":
                    ccfZIC = new CCFZIC();
                    ccfZIC.GetMaintenanceType();
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetMaintenanceType();
                    
                    break;
                case "SMSTemplate":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetSMSTemplate();
                    
                    break;
                case "CustomizedTroubleCode":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetCustomizedTroubleCode();
                    
                    break;
                case "RemindList":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetRemindList();
                    
                    break;
                case "SMSHistory":
                    this.radMaintenanceRecord.ItemsSource = customizeModel.GetSMSHistory();
                    break;
                
            }
            this.Dispatcher.BeginInvoke(new Action(() => this.radMaintenanceRecord.Rebind()));
        }

        private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
            if ((TableName == "VehicleType") || (TableName == "SMSHistory"))
            {
                menu.IsEnabled = false;
            }
            
        }

    }
}
